﻿using System;
using System.Collections.Generic;
using System.Text;
using Aricie.DNN.Modules.LuceneSearch.ModuleProviders;
using Aricie.DNN.Modules.LuceneSearch.Business;
using Lucene.Net.Documents;
using Aricie.XepientOpenDocLuceneProvider.Components.Business;
using XepientSolutions.XSDocumentLibrary;
using System.Collections;
using Aricie.Documents.Services;

namespace Aricie.XepientOpenDocLuceneProvider.Components.Lucene
{
    public class XepientDocumentLibraryProvider : ILuceneSearchableUpgrade, ILuceneFieldGlossary
    {
        #region Fields

        public class Fields
        {
            public const string DOCUMENT_NAME = "XSDocName";
            public const string DOCUMENT_DESCRIPTION = "XSDocDescription";
            public const string DOCUMENT_CONTENT = "XSDocContent";
            public const string DOCUMENT_EXTRACT = "XSDocContentExtract";
            public const string DOCUMENT_ROOT_FOLDER_NAME = "XSRootFolderName";
            public const string DOCUMENT_FOLDER_NAME = "XSFolderName";
            public const string DOCUMENT_DOWNLOAD_URL = "XSDocDownloadURL";
            public const string DOCUMENT_CATEGORIES = "XSDocCategories";
        }

        #endregion

        #region ILuceneSearchableUpgrade Membres

        public void UpgradeSearchItem(int portalId, ref Aricie.DNN.Modules.LuceneSearch.Business.LuceneSearchItemInfo searchItem)
        {
            XepientDocumentLibraryController providerController = new XepientDocumentLibraryController();
            int docId = Convert.ToInt32(searchItem.SearchKey);
            int hostUserId = providerController.GetHostUserId(portalId);

            FieldFactory factory = FieldFactory.Instance;

            XSDocumentLibraryController docController = new XSDocumentLibraryController();
            XSDocumentLibraryInfoDocument xsDoc = docController.SelectDocument(docId, hostUserId);

            if (xsDoc != null)
            {
                // DOCUMENT_NAME
                searchItem.AdditionalFields.Add(factory.CreateField(Fields.DOCUMENT_NAME, xsDoc.DocumentName));
                // DOCUMENT_DOWNLOAD_URL
                searchItem.AdditionalFields.Add(factory.CreateField(Fields.DOCUMENT_DOWNLOAD_URL, providerController.GetDownloadUrl(portalId, searchItem.ModuleInfo, docId)));
                // DOCUMENT_DESCRIPTION
                searchItem.AdditionalFields.Add(factory.CreateField(Fields.DOCUMENT_DESCRIPTION, xsDoc.DocumentDescription));
                // DOCUMENT_CATEGORIES
                ArrayList listCategories = docController.SelectCategoryByDocumentId(xsDoc.DocumentId);
                string categories = string.Empty;
                if (listCategories != null && listCategories.Count > 0)
                    foreach (XSDocumentLibraryInfoCategory cat in listCategories)
                        categories += cat.CategoryName + " ";
                searchItem.AdditionalFields.Add(factory.CreateField(Fields.DOCUMENT_CATEGORIES, categories.Trim()));

                XSDocumentLibraryInfoFolder xsDocFolder = docController.SelectFolder(xsDoc.FolderId, hostUserId);
                if (xsDocFolder != null)
                {
                    // DOCUMENT_FOLDER_NAME
                    searchItem.AdditionalFields.Add(factory.CreateField(Fields.DOCUMENT_FOLDER_NAME, xsDocFolder.FolderName));
                    List<XSDocumentLibraryInfoFolder> listTreeFolders = new List<XSDocumentLibraryInfoFolder>();
                    XSDocumentLibraryInfoFolder xsDocRootFolder = providerController.GetRootFolder(docController, xsDocFolder, hostUserId, ref listTreeFolders);
                    if (xsDocRootFolder != null)
                    {
                        // DOCUMENT_ROOT_FOLDER_NAME
                        searchItem.AdditionalFields.Add(factory.CreateField(Fields.DOCUMENT_ROOT_FOLDER_NAME, xsDocRootFolder.FolderName));
                    }

                    // ItemViewRoleIds ItemUserRoleIds
                    searchItem.InheritModuleViewPermissions = false;
                    searchItem.ItemViewRoleIds = providerController.ListRoleIdsView(portalId, docController, xsDocFolder);
                    searchItem.ItemUserRoleIds = providerController.ListUserIdsView(portalId, docController, xsDocFolder);

                    // DOCUMENT_CONTENT
                    string separator = string.Empty;
                    if (xsDocFolder.FolderPath[xsDocFolder.FolderPath.Length - 1] != Convert.ToChar("\\"))
                        separator = "\\";
                    string filePath = xsDocFolder.FolderPath + separator + xsDoc.DocumentName;
                    string fileContent = providerController.GetContentFromFile(filePath, 3000);

                    searchItem.AdditionalFields.Add(factory.CreateField(Fields.DOCUMENT_CONTENT, fileContent));

                    // DOCUMENT_EXTRACT
                    string extract = fileContent.Substring(0, ((fileContent.Length > 300) ? 300 : fileContent.Length));
                    searchItem.AdditionalFields.Add(factory.CreateField(Fields.DOCUMENT_EXTRACT, extract));
                }
            }
        }

        #endregion

        #region ILuceneFieldGlossary Membres

        public IList<FieldDefinition> GetFieldDefinitions()
        {
            List<FieldDefinition> toReturn = new List<FieldDefinition>();

            toReturn.Add(new FieldDefinition(Fields.DOCUMENT_NAME, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO, false, 1, false));
            toReturn.Add(new FieldDefinition(Fields.DOCUMENT_CONTENT, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO, true, 1, false));
            toReturn.Add(new FieldDefinition(Fields.DOCUMENT_EXTRACT, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO, true, 1, false));
            toReturn.Add(new FieldDefinition(Fields.DOCUMENT_ROOT_FOLDER_NAME, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO, true, 1, false));
            toReturn.Add(new FieldDefinition(Fields.DOCUMENT_FOLDER_NAME, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO, true, 1, false));
            toReturn.Add(new FieldDefinition(Fields.DOCUMENT_DOWNLOAD_URL, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO, true, 1, false));
            toReturn.Add(new FieldDefinition(Fields.DOCUMENT_CATEGORIES, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO, true, 1, false));

            return toReturn;
        }

        #endregion
    }
}
